package com.bjtu.literatureclient.controller.authorindex;


import com.bjtu.literatureclient.domain.authorindex.AuthorIndex;
import com.bjtu.literatureclient.domain.city.CityState;
import com.bjtu.literatureclient.domain.institution.CNInstitution;
import com.bjtu.literatureclient.domain.institution.ENInstitution;
import com.bjtu.literatureclient.domain.journal.JournalChinese;
import com.bjtu.literatureclient.domain.journal.JournalEnglish;
import com.bjtu.literatureclient.domain.publisher.CNPublisher;
import com.bjtu.literatureclient.domain.publisher.ENPublisher;
import com.bjtu.literatureclient.domain.user.User;
import com.bjtu.literatureclient.domain.user.UserIndexNoTable;
import com.bjtu.literatureclient.service.authorindex.AuthorIndexService;
import com.bjtu.literatureclient.service.publisher.ICNPublisherService;
import com.google.gson.Gson;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//接收客户端的请求，然后调用Service层业务逻辑，获取到数据，传递数据给视图层（客户端）用于视觉呈现
@RestController
//Controller负责处理的，根目录下的URL ，/user/** 下的所有路径都会被Controller所拦截  RestController和Controller区别是 RestController 默认在他的方法上全加上了@ResponseBody
@RequestMapping("/AuthorIndex")
public class AuthorIndexController {	
	@Autowired
	private AuthorIndexService authorIndexService;
	private ICNPublisherService CNPublisherService;
	public String username;//用户号
	int count=1;
	//使该方法负责处理/AuthorIndex/addAuthorIndex 这个url 并且是由post方法方法传递过来的请求
	@RequestMapping(value="/addAuthorIndex" ,method = RequestMethod.POST)
	//自动序列化成json
	//@ResponseBody
	public boolean addAuthorIndex(HttpServletRequest request){
		boolean flag=true;
		//前端Ajax传来的参数
		String id=request.getParameter("id");
		System.out.println("id:"+id);
		int authorOrder=Integer.parseInt(request.getParameter("getCount"));//序号
		System.out.println("转换为的authorOrder:"+authorOrder);
		String surName=request.getParameter("surName");  
		String Name=request.getParameter("Name");
		String email=request.getParameter("Email");//电子邮箱
		String indexNo=request.getParameter("IndexNo");//索引号
		String middleName="";
		String indexCode="";
		String lastName="";
		String firstName="";
		AuthorIndex authorIndex=new AuthorIndex();//生成表的对象
		System.out.println("前端传来的数据：期刊id:"+id+"\tauthorOrder:"+authorOrder+"\tsurName:"+surName+"\tName:"+Name
				 +"\temail:"+email+"\tindexNo:"+indexNo);
		if(id.equals("2")) {//中文期刊的话
			//全名
			System.out.println("中文期刊作者索引表添加!");
			middleName=surName+Name;//姓名
			indexCode="JB";//文献代码，中文期刊JB，西文期刊JA
			//调用service中的方法，由汉字转为拼音的方法
			lastName=authorIndexService.getSurName(surName);//拼音姓氏
			firstName = null;//判断名字是两个字还是三个字
			if(Name.length()==1)
				firstName=authorIndexService.getName(Name.substring(0,1));			
			else if(Name.length()>1)
				firstName=authorIndexService.getName(Name.substring(0,1))+"-"+authorIndexService.getName(Name.substring(1));//拼音名
			//设定值
			authorIndex.setAuthorOrder(authorOrder);
			authorIndex.setEmail(email);
			authorIndex.setFirstName(firstName);
			authorIndex.setIndexCode(indexCode);
			authorIndex.setIndexNo(indexNo);
			authorIndex.setLastName(lastName);
			authorIndex.setMiddleName(middleName);
			//输出测试一下，从前端传来的数据是否正确
			System.out.println("中文期刊作者索引表条目：authorOrder:"+authorOrder+"\tsurName:"+surName+"\tName:"+Name
					 +"\temail:"+email+"\tindexCode:"+indexCode+"\tfirstName:"+firstName
					 +"\tlastName:"+lastName+"\tmiddleName:"+middleName);
			//调用服务，将数据添加到表中		 
				
		}
		else if(id.equals("1")) {//英文期刊的话
			//全名
			System.out.println("西文期刊作者索引表添加!");
			middleName=surName+" "+Name;//姓名，全称
			firstName=Name;//作者名			
			lastName=surName;//作者姓的长度surName.length  或者姓氏的长度不超过30
			if(surName.length()>30) {
				lastName=surName.substring(0,30);//作者姓截取前30个西文字符
			}			
			indexCode="JA";//文献代码，中文期刊JB，西文期刊JA
			//设定值
			authorIndex.setAuthorOrder(authorOrder);
			authorIndex.setEmail(email);
			authorIndex.setFirstName(firstName);
			authorIndex.setIndexCode(indexCode);
			authorIndex.setIndexNo(indexNo);
			authorIndex.setLastName(lastName);
			authorIndex.setMiddleName(middleName);
			//输出测试一下，从前端传来的数据是否正确
			System.out.println("西文期刊作者索引表条目：authorOrder:"+authorOrder+"\tindexNo:"+indexNo+"\tfirstName:"+firstName
					+"\tlastName"+lastName+"\tmiddleName:"+middleName
					+"\temail:"+email+"\tindexCode:"+indexCode
					 );					
		}
		//调用服务，将数据先添加到用户索引表中
		/*
		if(count==1) {
			Subject currentUser = SecurityUtils.getSubject();
			Session session = currentUser.getSession();
			User user=(User) session.getAttribute("userInfo");			
			String UserNo=user.getUserNo();		
			String Item="无";			
			int Status=1;		
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String Stamp=df.format(new Date());
			System.out.println("用户索引表中条目：UserNo:"+UserNo+"\tIndexNo:"+indexNo+"\tItem:"+Item+
					"\tIndexCode:"+indexCode+"\tStatus:"+Status+"\tStamp:"+Stamp);
			UserIndexNoTable userIndexNoTable=new UserIndexNoTable();
			userIndexNoTable.setUserNo(UserNo);
			userIndexNoTable.setIndexNo(indexNo);
			userIndexNoTable.setItem(Item);
			userIndexNoTable.setIndexCode(indexCode);
			userIndexNoTable.setItatus(Status);
			userIndexNoTable.setStamp(Stamp);
			boolean flag1=authorIndexService.addUSerIndex(userIndexNoTable);
			if(flag1==true)
			{
				System.out.println("用户索引表插入成功");
				flag=authorIndexService.addAuthorIndex(authorIndex);
			}
			count++;
		}
		*/
		//Gson gson = new Gson();
		flag=authorIndexService.addAuthorIndex(authorIndex);
		return flag;//gson.toJson(authorIndex)	
	}	
	
	@RequestMapping(value="/addJournalTable" ,method = RequestMethod.POST)
	//自动序列化成json
	@ResponseBody
	public Boolean addJournalTable(HttpServletRequest request) {
		String IndexNo=request.getParameter("IndexNo");				
		String ISSN=request.getParameter("ISSN");
		int FoundYear=Integer.parseInt(request.getParameter("FoundYear"));
		String ReleaseCycles=request.getParameter("ReleaseCycles");
		String CN=request.getParameter("CN");		
		String Note=request.getParameter("Note");
		String JournalEmail=request.getParameter("JournalEmail");
		String id=request.getParameter("id");	
		Boolean flag=true;
		if(id.equals("1")) {//中文名称 换成了journalName1是英文期刊名称的缩写
			 String JournalNameAbbr=request.getParameter("JournalNameAbbr");//中文名称
			 String journalName1=request.getParameter("EnglishName");
			 String InstitutionName=request.getParameter("InstitutionName");
			 int InstitutionID=authorIndexService.getInstitutionID1(InstitutionName);
			 System.out.println("InstitutionID:"+InstitutionID);
			 System.out.println("Controller,IndexNo:"+IndexNo+"\tid:"+id+"\tJournalNameAbbr:"+JournalNameAbbr+"\tjournalName1:"+journalName1+
						"\tISSN:"+ISSN+"\tFoundYear:"+FoundYear+"\tReleaseCycles:"+ReleaseCycles+"\tCN:"+CN+"\tInstitutionID:"+InstitutionID+"\tNote:"+Note);
			 JournalEnglish journalEnglish=new JournalEnglish(IndexNo,journalName1,ISSN,JournalNameAbbr,InstitutionID,FoundYear,ReleaseCycles,JournalEmail,Note);
			 journalEnglish.setIndexNo(IndexNo);
			 journalEnglish.setJournalName(journalName1);//英文名称
			 journalEnglish.setiSSN(ISSN);
			 journalEnglish.setJournalNameAbbr(JournalNameAbbr);//英文期刊的缩写名
			 journalEnglish.setInstitutionID(InstitutionID);
			 journalEnglish.setFoundYear(FoundYear);
			 journalEnglish.setReleaseCycles(ReleaseCycles);
		     journalEnglish.setJournalEmail(JournalEmail);
			 journalEnglish.setNote(Note);
			 flag=authorIndexService.addJournalTable1(journalEnglish);
		 }
		 else if(id.equals("2")) {//中文
			String journalName2=request.getParameter("ChineseName");//中文名称
			String journalName1=request.getParameter("EnglishName");
			String InstitutionName2=request.getParameter("InstitutionName2");
			int InstitutionID=authorIndexService.getInstitutionID(InstitutionName2);
			System.out.println("InstitutionID:"+InstitutionID);
			System.out.println("Controller,IndexNo:"+IndexNo+"\tid:"+id+"\tjournalName2:"+journalName2+"\tjournalName1:"+journalName1+
					"\tISSN:"+ISSN+"\tFoundYear:"+FoundYear+"\tReleaseCycles:"+ReleaseCycles+"\tCN:"+CN+"\tInstitutionName2:"+InstitutionName2+"\tJournalEMail:"+JournalEmail+"\tNote:"+Note);
			JournalChinese journalChinese=new JournalChinese();
			journalChinese.setIndexNo(IndexNo);
			journalChinese.setJournalName1(journalName1);
			journalChinese.setJournalName2(journalName2);
			journalChinese.setiSSN(ISSN);
			journalChinese.setInstitutionID(InstitutionID);
			journalChinese.setFoundYear(FoundYear);			
			journalChinese.setReleaseCycles(ReleaseCycles);			
			journalChinese.setCN(CN);
			journalChinese.setNote(Note);
			journalChinese.setJournalEmail(JournalEmail);
			flag=authorIndexService.addJournalTable2(journalChinese);								
		 }
		 return flag;
	}
	@RequestMapping(value="/checkISSN" ,method = RequestMethod.POST)
	//自动序列化成json
	//@ResponseBody
	public Boolean checkISSN(HttpServletRequest request) {
		String Issn=request.getParameter("ISSN");
		String id=request.getParameter("id");
		System.out.println("checkISSN	id:"+id+"\tISSN:"+Issn);
		Boolean flag=true;
		if(id.equals("2"))//中文期刊检测
			 flag=authorIndexService.checkISSN(Issn);
		else if(id.equals("1"))//英文
			flag=authorIndexService.checkISSN1(Issn);
		return flag;		
	}
	@RequestMapping(value="/addInstitutionTable" ,method = RequestMethod.POST)
	//自动序列化成json
	//@ResponseBody
	public Boolean addInstitutionTable(HttpServletRequest request) {
		String Country=request.getParameter("Country");
		String Province=request.getParameter("Province");
		String City=request.getParameter("City");
		String id=request.getParameter("id");
		System.out.println("id===="+id);
		int CityID=0;
		if(id.equals("2"))//中文
			 CityID=authorIndexService.getCityID(Country,Province,City);
		else if(id.equals("1"))//英文
			CityID=authorIndexService.getCityID1(Country,Province,City);
		String InstitutionWebsite=request.getParameter("InstitutionWebsite");
		String InstitutionAddress=request.getParameter("InstitutionAddress");
		String Note=request.getParameter("Note");			
		Boolean flag=true;
		if(id.equals("2")) {			
			//中文
			String InstitutionName2=request.getParameter("InstitutionName2");
			String InstitutionName1=request.getParameter("InstitutionName1");
			System.out.println("id:"+id+"\tInstitutionName2:"+InstitutionName2+"\tInstitutionName1:"+InstitutionName1+
					"\tInstitutionWebsite:"+InstitutionWebsite+"\tProvince:"+Province+"\tCity:"+City+"\tCityID:"+CityID+
					"\tInstitutionAddress:"+InstitutionAddress+"\tNote:"+Note);	
			CNInstitution institution=new CNInstitution();
			institution.setInstitutionName2(InstitutionName2);
			institution.setInstitutionName1(InstitutionName1);
			institution.setInstitutionWebsite(InstitutionWebsite);
			institution.setCityID(CityID);
			institution.setInstitutionAddress(InstitutionAddress);
			institution.setNote(Note);
			flag=authorIndexService.addInstitutionTable2(institution);
		}
		else if(id.equals("1")) {
			//英文			
			String InstitutionName=request.getParameter("InstitutionName");
			System.out.println("id:"+id+"\tInstitutionName:"+InstitutionName+
					"\tInstitutionWebsite:"+InstitutionWebsite+"\tProvince:"+Province+"\tCity:"+City+"\tCityID:"+CityID+
					"\tInstitutionAddress:"+InstitutionAddress+"\tNote:"+Note);
			ENInstitution institution=new ENInstitution();
			institution.setInstitutionName(InstitutionName);
			institution.setInstitutionWebsite(InstitutionWebsite);
			institution.setCityID(CityID);
			institution.setInstitutionAddress(InstitutionAddress);
			institution.setNote(Note);
			flag=authorIndexService.addInstitutionTable1(institution);			
		}			
		return flag;		
	}
	@RequestMapping(value="/addPublisherTable" ,method = RequestMethod.POST)
	//自动序列化成json
	//@ResponseBody
	public Boolean addPublisherTable(HttpServletRequest request) {
		String PublisherID=request.getParameter("PublisherID");				
		String Country=request.getParameter("Country");
		String Province=request.getParameter("Province");
		String City=request.getParameter("City");
		String id=request.getParameter("id");
		System.out.println("id===="+id);
		int CityID=0;
		if(id.equals("2"))//中文
			 CityID=authorIndexService.getCityID(Country,Province,City);
		else if(id.equals("1"))//英文
			CityID=authorIndexService.getCityID1(Country,Province,City);
		String website=request.getParameter("website");
		String PublisherAddress=request.getParameter("PublisherAddress");
		String Note=request.getParameter("Note");	
		Boolean flag=true;
		if(id.equals("2")) {			
			//中文
			String PublisherName2=request.getParameter("PublisherName2");
			String PublisherName1=request.getParameter("PublisherName1");
			System.out.println("id:"+id+"\tPublisherID:"+PublisherID+"\tPublisherName2:"+PublisherName2+"\tPublisherName1:"+PublisherName1+
					"\tCountry:"+Country+"\tProvince:"+Province+"\tCity:"+City+"\tCityID:"+CityID+
					"\twebsite:"+website+"\tpublisherAddress:"+PublisherAddress+"\tNote:"+Note);
			CNPublisher cnPublisher=new CNPublisher();//西文出版商
			cnPublisher.setPublisherID(PublisherID);
			cnPublisher.setPublisherName1(PublisherName1);//出版商缩写
			cnPublisher.setPublisherName2(PublisherName2);
			cnPublisher.setPublisherAddress(PublisherAddress);
			cnPublisher.setCityID(CityID);
			cnPublisher.setWebsite(website);
			cnPublisher.setNote(Note);
			flag=authorIndexService.addPublisherTable2(cnPublisher);
		}
		else if(id.equals("1")) {
			//英文
			String PublisherNameAbbr=request.getParameter("PublisherNameAbbr");
			String PublisherName1=request.getParameter("PublisherName1");
			System.out.println("id:"+id+"\tPublisherID:"+PublisherID+"\tPublisherNameAbbr:"+PublisherNameAbbr+"\tPublisherName1:"+PublisherName1+
					"\tCountry:"+Country+"\tProvince:"+Province+"\tCity:"+City+"\tCityID:"+CityID+
					"\twebsite:"+website+"\tpublisherAddress:"+PublisherAddress+"\tNote:"+Note);
			ENPublisher enPublisher=new ENPublisher();//西文出版商
			enPublisher.setPublisherID(PublisherID);
			enPublisher.setPublisherNameAbbr(PublisherNameAbbr);//出版商缩写
			enPublisher.setPublisherName(PublisherName1);
			enPublisher.setPublisherAddress(PublisherAddress);
			enPublisher.setCityID(CityID);
			enPublisher.setWebsite(website);
			enPublisher.setNote(Note);
			flag=authorIndexService.addPublisherTable1(enPublisher);
		}			
		return flag;		
	}
	@RequestMapping(value="/addCityStateTable" ,method = RequestMethod.POST)
	//自动序列化成json
	//@ResponseBody
	public Boolean addCityStateTable(HttpServletRequest request) {
		String id=request.getParameter("id");
		String Region=request.getParameter("Region");//地区
		String StateName=request.getParameter("StateName2");//中文期刊传来的是中文的州或省的名字、英文期刊传来的是英文的州和省的名字
		System.out.println("添加城市表：StateName:"+StateName);
		String CityName2=request.getParameter("CityName2");		
		String CityName1=request.getParameter("CityName1");
		//System.out.println("Region:"+Region+"\tStateName2:"+StateName2+"\tCityName2:"+CityName2+"\tCityName1:"+CityName1);		
		CityState cityState=new CityState();
		if(id.equals("2"))//中文期刊
		{
			String StateName1=authorIndexService.getEnglishStateName1(StateName);//转为英文的名字
			System.out.println("中文期刊城市添加，转化英文的州或省的名称为："+StateName1);				
			cityState.setRegion(Region);
			cityState.setStateName2(StateName);//设置英文的州或省的名字
			cityState.setStateName1(StateName1);
			cityState.setCityName1(CityName1);
			cityState.setCityName2(CityName2);
			System.out.println("Region:"+Region+"\tStateName:"+StateName+"\tStateName1:"+StateName1+"\tCityName2:"+CityName2+"\tCityName1:"+CityName1);	
		}
		else if(id.equals("1")) {
			String StateName2=authorIndexService.getChineseStateName2(StateName);//转为英文的名字
			System.out.println("英文期刊城市表添加：转化中文的州或省的名称为："+StateName2);				
			cityState.setRegion(Region);
			cityState.setStateName2(StateName2);//设置英文的州或省的名字
			cityState.setStateName1(StateName);
			cityState.setCityName1(CityName1);
			cityState.setCityName2(CityName2);
			System.out.println("Region:"+Region+"\tStateName2:"+StateName2+"\tStateName:"+StateName+"\tCityName2:"+CityName2+"\tCityName1:"+CityName1);	
		}
		
		Boolean flag=authorIndexService.addCityStateTable(cityState);		
		return flag;		
	}
	@RequestMapping(value="/getStateName" ,method = RequestMethod.POST)
	public String getStateName(HttpServletRequest request) {
		String Region=request.getParameter("Region");
		String id=request.getParameter("id");
		System.out.println("得到州：id:"+id+"\tRegion:"+Region);
		List <String> arrayList=new ArrayList <String>();
		arrayList=authorIndexService.getStateName2(Region,id);		//省	
	    /*Iterator iter=arrayList.iterator();
	    while(iter.hasNext()) {
	    	System.out.print(iter.next()+",");
	    }
	    System.out.println();
	    System.out.println("arrayList:"+arrayList);*/
	    Gson gson=new Gson();//转为json
	    String info=gson.toJson(arrayList);
        System.out.println("info:"+info);
		return info;
	}
	@RequestMapping(value="/getCityName" ,method = RequestMethod.POST)
	public String getCityName(HttpServletRequest request) {
		String StateName2=request.getParameter("StateName2");
		String id=request.getParameter("id");
		System.out.println("设置城市：id:"+id+"\t州的名字：StateName2:"+StateName2);
		List <String> arrayList=new ArrayList <String>();
		if(id.equals("1")) {
			arrayList=authorIndexService.getCityName1(StateName2);
		}
		else if(id.equals("2")) {
			arrayList=authorIndexService.getCityName2(StateName2);		//省
		}	
	    Iterator iter=arrayList.iterator();
	    while(iter.hasNext()) {
	    	System.out.print(iter.next()+",");
	    }
	    System.out.println();
	    System.out.println("arrayList:"+arrayList);
	    Gson gson=new Gson();//转为json
	    String info=gson.toJson(arrayList);
        System.out.println("info:"+info);
		return info;
	}
	@RequestMapping(value="/getPublisher" ,method = RequestMethod.POST)
	public String getPublisher(HttpServletRequest request) {
		System.out.println("进入getPublisher Controller层");
		List <String> arrayList=new ArrayList <String>();
		arrayList=authorIndexService.getPublisher();//得到Publisher
	    Iterator iter=arrayList.iterator();
	    while(iter.hasNext()) {
	    	System.out.print(iter.next()+",");
	    }
	    System.out.println();
	    System.out.println("arrayList:"+arrayList);
	    Gson gson=new Gson();//转为json
	    String info=gson.toJson(arrayList);
        System.out.println("info:"+info);
		return info;
	}
	@RequestMapping(value="/getPublisher1" ,method = RequestMethod.POST)
	public String getPublisher1(HttpServletRequest request) {
		System.out.println("进入getPublisher1 Controller层");
		List <String> arrayList=new ArrayList <String>();
		arrayList=authorIndexService.getPublisher1();//得到Publisher
	    Iterator iter=arrayList.iterator();
	    while(iter.hasNext()) {
	    	System.out.print(iter.next()+",");
	    }
	    System.out.println();
	    System.out.println("arrayList:"+arrayList);
	    Gson gson=new Gson();//转为json
	    String info=gson.toJson(arrayList);
        System.out.println("info:"+info);
		return info;
	}
	@RequestMapping(value="/getusername" ,method = RequestMethod.POST)//加入用户索引表
	//自动序列化成json
	@ResponseBody
	public Boolean getusername(HttpServletRequest request) {
		boolean flag=true;
		String username=request.getParameter("username");
		System.out.println("username:"+username);
		if(username.equals(null))
			flag=false;
		System.out.println("controller层flag:"+flag);
		return flag;		
	}
	@RequestMapping(value="/addUSerIndex" ,method = RequestMethod.POST)//加入用户索引表
	//自动序列化成json
	@ResponseBody
	public Boolean addUSerIndex(HttpServletRequest request) {//前端调用addUserIndex
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user=(User) session.getAttribute("userInfo");
		
		String UserNo=user.getUserNo();
		System.out.println("用户索引表，当前登录的用户UserNo:"+UserNo);
		String IndexNo=request.getParameter("IndexNo");
		String Item=request.getParameter("Item");	
		String IndexCode=request.getParameter("IndexCode");
		int Status=Integer.parseInt(request.getParameter("Status"));
		String Stamp=request.getParameter("Stamp");
		System.out.println("用户索引表中条目：UserNo:"+UserNo+"\tIndexNo:"+IndexNo+"\tItem:"+Item+
				"\tIndexCode:"+IndexCode+"\tStatus:"+Status+"\tStamp:"+Stamp);
		UserIndexNoTable userIndexNoTable=new UserIndexNoTable();
		userIndexNoTable.setUserNo(UserNo);
		userIndexNoTable.setIndexNo(IndexNo);
		userIndexNoTable.setItem(Item);
		userIndexNoTable.setIndexCode(IndexCode);
		userIndexNoTable.setItatus(Status);
		userIndexNoTable.setStamp(Stamp);
		boolean flag=authorIndexService.addUSerIndex(userIndexNoTable);
		
		return flag;		
	}
	
}
